Writing your first program using Eclipse

To begin, startup Eclipse. You can do this by typing eclipse & into the command prompt

 

Next, you will be asked to select a workspace. This is important! This is the root directory to which all of your programs will be saved to, so make sure you know where it is. In my case, I clicked Browse... and created a directory called eecs2030, but you can use any directory name that you like (including the default one workspace); you should avoid spaces in the directory name.

 

Once Eclipse starts up, if you see this welcome screen, click on the orange Workbench button found near the top right corner.

 

Now we need to create a Java Project. You can do this by doing: File -> New... -> Project.... The project wizard looks like this - click on Java Project.

 

Now, you will see a window like the one shown. I have named my project 'Tutorial'. You can name your project whatever you like, but avoid using spaces in the project name because this complicates navigating the directory structure of your project. Note that doing this now creates a directory in your previously created workspace directory. Keep the default settings and click finish.

 

You may then see a popup window like the one showed here. Click Yes if that is the case. This will configure eclipse so that it enables Java specific features.

 

The next step is to create a package. To do this, right-click the new project you just created that will now appear in the Package Explorer on the left side, and then click New -> Package.

 

Packages are nothing more than directories. This will be useful for when your projects become very large. Also, note that there is a naming convention for packages that we are not demonstrating here. To learn more about packages, refer to this Java Tutorial. But for now, our package named eecs2030 serves our purposes adequately.

 

Now if you look in the Package Explorer, our eecs2030 package is under Tutorial -> src -> eecs2030. src is the source folder. Hence, when it comes time to submit your files and you want to locate your source file, it will be in workspace->Project->src->package->file. So in our case, it would be: eecs2030/Tutorial/src/eecs2030/HelloWorld.java

Now, to finally create our HelloWorld.java, we must create a class. You can do this by right-clicking the package, then clicking New, and then Class.

 

You will see this window. Enter the name of your class, in this case HelloWorld, and check the public static void main(String[] args) box - this will create the main method in your class for you. Our simple program does not inherit from any other program, so the other two boxes don't really matter.

 

You should now see something like the image to the right. The TODO comment is unnecessary, so you can delete it.

 

Type in System.out.print("Hello, world."); into the main body and then save your program by pressing ctrl+s on your keyboard.

 

Now you can run your program by either clicking the green run button on the toolbar, or by right-clicking your package in the package explorer and selecting Run As...->Java Application

Now your output will display in the console at the bottom!

Now, if you want to submit this file, open up a console. By default, you will be in your home directory (/cse/home/##### - where ##### is your EECS user name). Recall that we created our workspace, called eecs2030 in the home directory. We first go into our workspace directory by typing cd eecs2030

Now, if you type ls, you will see the contents of your workspace, which, for now, consists of the project we created named Tutorial.

Go into the Tutorial directory by typing cd Tutorial.

Type ls to list the contents of the directory. You should see two directories named bin and src. The directory named src is where your Java source code files are located.

Go into the src directory by typing cd src.

Type ls to list the contents of the directory. You should see one directory named eecs2030 which is the name of the package that we created.

Go into the eecs2030 directory by typing cd eecs2030.

Type ls to list the contents of the directory. You should see one file named HelloWorld.java which is the name of the Java class that we created.

Finally, to submit your Java file you use the submit command:

submit 2030 lab0 HelloWorld.java

This electronically submits the file named HelloWorld.java to the course 2030 for the assignment named lab0. If your file is successfully submitted, then you will see the message All files successfully submitted and nothing else. If something is wrong with your submission, you will see a detailed error message explaining what is wrong with your submission.

 

Goto the last part of this lab...